// Mermaid 图表配置文件
// 提供所有页面共享的Mermaid配置和主题管理

/**
 * 预设主题配置
 */
export const presetThemes = {
    default: {
        theme: 'default',
        themeVariables: {
            primaryColor: '#667eea',
            primaryTextColor: '#fff',
            primaryBorderColor: '#667eea',
            lineColor: '#667eea',
            secondaryColor: '#f5f7fa',
            tertiaryColor: '#e0e0e0'
        }
    },
    dark: {
        theme: 'dark',
        themeVariables: {
            primaryColor: '#8884d8',
            primaryTextColor: '#fff',
            primaryBorderColor: '#8884d8',
            lineColor: '#8884d8',
            secondaryColor: '#2c3e50',
            tertiaryColor: '#34495e',
            backgroundColor: '#1a1a1a',
            textColor: '#e0e0e0'
        }
    },
    forest: {
        theme: 'forest',
        themeVariables: {
            primaryColor: '#4caf50',
            primaryTextColor: '#fff',
            primaryBorderColor: '#4caf50',
            lineColor: '#4caf50',
            secondaryColor: '#f1f8e9',
            tertiaryColor: '#c8e6c9'
        }
    },

    industrial: {
        theme: 'default',
        themeVariables: {
            primaryColor: '#7d8c4b',
            primaryTextColor: '#f8f9fa',
            primaryBorderColor: '#a0a0a0',
            lineColor: '#596275',
            secondaryColor: '#f5f5f5',
            tertiaryColor: '#e0e0e0',
            nodeColor: '#7d8c4b',
            nodeBorder: '#a0a0a0',
            clusterBkg: '#f5f5f5',
            defaultLinkColor: '#596275'
        }
    },
    cyberpunk: {
        theme: 'dark',
        themeVariables: {
            primaryColor: '#ff0055',
            primaryTextColor: '#00ffc5',
            primaryBorderColor: '#ff0055',
            lineColor: '#00ffc5',
            secondaryColor: '#0f0e17',
            tertiaryColor: '#242629',
            backgroundColor: '#000000',
            textColor: '#fffffe',
            nodeColor: '#ff0055',
            nodeBorder: '#ff0055',
            clusterBkg: '#0f0e17',
            defaultLinkColor: '#00ffc5'
        }
    },
    minimalist: {
        theme: 'default',
        themeVariables: {
            primaryColor: '#404040',
            primaryTextColor: '#ffffff',
            primaryBorderColor: '#666666',
            lineColor: '#888888',
            secondaryColor: '#ffffff',
            tertiaryColor: '#f5f5f5',
            nodeColor: '#404040',
            nodeBorder: '#666666',
            clusterBkg: '#ffffff',
            defaultLinkColor: '#888888'
        }
    },
    greenit: {
        theme: 'greenit',
        themeVariables: {
            primaryColor: '#2e7d32',
            primaryTextColor: '#ffffff',
            primaryBorderColor: '#388e3c',
            lineColor: '#43a047',
            secondaryColor: '#f1f8e9',
            tertiaryColor: '#c8e6c9',
            nodeColor: '#2e7d32',
            nodeBorder: '#388e3c',
            clusterBkg: '#f1f8e9',
            defaultLinkColor: '#43a047'
        }
    }
};

/**
 * Mermaid图表的默认配置
 */
export const defaultMermaidConfig = {
    ...presetThemes.default,
    flowchart: {
        useMaxWidth: false,
        htmlLabels: true
    },
    sequenceDiagram: {
        useMaxWidth: false
    },
    gantt: {
        useMaxWidth: false
    },
    classDiagram: {
        useMaxWidth: false
    }
};

/**
 * 获取当前应用的主题
 * @returns {string} 当前主题名称
 */
export function getCurrentTheme() {
    return localStorage.getItem('mermaid-draw-theme') || 'default';
}

/**
 * 保存当前应用的主题
 * @param {string} themeName 主题名称
 */
export function saveCurrentTheme(themeName) {
    localStorage.setItem('mermaid-draw-theme', themeName);
}

/**
 * 初始化Mermaid配置
 * @param {Object|string} configOrTheme - 自定义配置对象或主题名称
 */
export function initMermaid(configOrTheme = {}) {
    if (!window.mermaid) {
        return false;
    }
    
    let mergedConfig = { ...defaultMermaidConfig };
    
    if (typeof configOrTheme === 'string') {
        // 如果传入的是主题名称
        const themeConfig = presetThemes[configOrTheme];
        if (themeConfig) {
            mergedConfig = {
                ...mergedConfig,
                ...themeConfig,
                themeVariables: {
                    ...mergedConfig.themeVariables,
                    ...themeConfig.themeVariables
                }
            };
        }
    } else if (typeof configOrTheme === 'object') {
        // 如果传入的是配置对象
        mergedConfig = {
            ...mergedConfig,
            ...configOrTheme,
            themeVariables: {
                ...mergedConfig.themeVariables,
                ...(configOrTheme.themeVariables || {})
            }
        };
    }
    
    window.mermaid.initialize(mergedConfig);
    return true;
}

/**
 * 更新Mermaid主题并重新渲染图表
 * @param {string|Object} themeOrConfig - 主题名称或配置对象
 */
export function updateMermaidTheme(themeOrConfig) {
    const success = initMermaid(themeOrConfig);
    if (success) {
        // 保存主题到本地存储
        if (typeof themeOrConfig === 'string') {
            saveCurrentTheme(themeOrConfig);
        }
        
        // 重新渲染所有Mermaid图表
        if (window.mermaid) {
            const mermaidElements = document.querySelectorAll('.mermaid');
            window.mermaid.init(undefined, mermaidElements);
        }
    }
    return success;
}

/**
 * 获取图表类型对应的图标类名
 * @param {string} chartType - 图表类型
 * @returns {string} - 图标类名
 */
export function getChartTypeIcon(chartType) {
    const iconMap = {
        'flowchart': 'chart-flow',
        'sequence': 'chart-sequence',
        'gantt': 'chart-gantt',
        'class': 'chart-class',
        'state': 'chart-state',
        'er': 'chart-er',
        'pie': 'chart-pie',
        'git': 'chart-git'
    };
    
    return iconMap[chartType] || 'chart-default';
}